# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : __new__单例模式(数据库连接池).py
# @Author: dongguangwen
# @Date  : 2025-03-29 22:32
class Singleton(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super().__new__(cls)
        return cls.__instance


class ConnectionPool(Singleton):
    def __init__(self):
        if not hasattr(self, 'pool'):
            self.pool = self._create_pool()

    @staticmethod
    def _create_pool():
        # 创建数据库连接池
        return "Connection Pool Object"


# 全局共享连接池
pool1 = ConnectionPool()
pool2 = ConnectionPool()
assert pool1.pool is pool2.pool  # True
print(pool1.pool is pool2.pool)  # True


"""
避免频繁创建/销毁连接，提升性能。
"""